package com.eqdd.yiqidian.ui.gongneng.renshi.zuzhiguanli;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.yiqidian.InvitePersonToComFromPhoneActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.CareerListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.listener.OnMyClickListener;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;

public class InvitePersonToComFromPhoneActivity extends BaseActivity {
    private InvitePersonToComFromPhoneActivityCustom dataBinding;
    private ArrayList<String> datas;
    private SlimAdapterEx slimAdapterEx;
    private String departmentname;
    private String departmentId;
    private String careerName;
    private String careerId;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_invite_person_to_com_from_phone);
        initTopTitleBar(View.VISIBLE, "邀请入职");
        initTopRightText("邀请", v -> {
            String phones = getPhones();
            if (!TextUtils.isEmpty(phones)) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.HR_INVITE)
                        .params("phones", phones)
                        .params("companyId", user.getCompanyId())
                        .params("company", user.getCompany())
                        .params("departId", departmentId)
                        .params("department", departmentname)
                        .params("postId", careerId)
                        .params("post", careerName)
                        .params("userGuid", user.getGuid())
                        .params("user", user.getUname())
                        .execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort("请先选择至少一名人员");
            }
        });
    }

    private String getPhones() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < datas.size(); i++) {
            stringBuilder.append(datas.get(i) + ";");
        }
        return stringBuilder.deleteCharAt(stringBuilder.lastIndexOf(";")).toString();
    }

    @Override
    public void initData() {
        departmentname = getIntent().getStringExtra(Config.DEPARTMENT_NAME);
        departmentId = getIntent().getStringExtra(Config.DEPARTMENT_ID);
        careerName =getIntent().getStringExtra(Config.CAREER_NAME);
        careerId = getIntent().getStringExtra(Config.CAREER_ID);

        datas = new ArrayList<>();
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_30, new SlimInjector<String>() {
            @Override
            public void onInject(String data, IViewInjector injector) {
                injector.text(R.id.tv_number, data)
                        .clicked(R.id.bt_delete, new OnMyClickListener(slimAdapterEx.getData().indexOf(data)) {
                            @Override
                            public void onClick(View v) {
                                slimAdapterEx.getData().remove(getPosition());
                                slimAdapterEx.notifyItemRemoved(getPosition());
                            }
                        });
            }
        }).attachTo(dataBinding.recyclerView);
        slimAdapterEx.updateData(datas);
    }

    @Override
    public void setView() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.bt_add:
                if (checkParam()) {
                    slimAdapterEx.getData().add(dataBinding.etNumber.getText().toString().trim());
                    slimAdapterEx.notifyItemInserted(slimAdapterEx.getData().size() - 1);
                }
        }
    }

    private boolean checkParam() {
        return !TextUtils.isEmpty(dataBinding.etNumber.getText());
    }
}
